// pages/component/thresItem/thresItem.js
const api = require('../../../apis/request.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    threshold:{
      type: Object,
      value: undefined
    },
    description: {
      type: String,
      value: "0"
    },
    tMax: {
      type: String,
      value: "0"
    },
    tMin: {
      type: String,
      value: "0"
    },
    tDiff: {
      type: String,
      value: "0"
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    tempTMax:"",
    tempTMin:"",
    tempTDiff:"",
    old_obj:{}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    saveThres: function (event) {
      var _this=this;
      // console.log(JSON.stringify(_this.data.threshold));
      // JSON.stringify(_this.data.threshold)
      _this.data.old_obj = { ..._this.data.threshold}
      var obj = _this.data.threshold
      obj.tMax = _this.data.tempTMax || _this.data.old_obj.tMax
      obj.tMin = _this.data.tempTMin || _this.data.old_obj.tMin
      obj.tDiff = _this.data.tempTDiff || _this.data.old_obj.tDiff
     
      console.log(_this.data.threshold)
      console.log(obj)
      api.addThreshold(JSON.stringify(obj),function(res){
        // console.log(JSON.stringify(res.data));
        var result = res.data;
        if(result.status!=0){
          wx.showToast({
            title: result.msg
          });
          
          // if (_this.data.tempTMax != "") {
          //   console.log("tempTMax = " + _this.data.tempTMax);
          //   _this.data.threshold.tMax = _this.data.tempTMax;
          //   _this.data.tempTMax="";
          // }
          // if (_this.data.tempTMin != "") {
          //   console.log("tempTMin = " + _this.data.tempTMin);
          //   _this.data.threshold.tMin = _this.data.tempTMin;
          //   _this.data.tempTMin="";
          // }
          // if (_this.data.tempTDiff != "") {
          //   console.log("tempTDiff = " + _this.data.tempTDiff);
          //   _this.data.threshold.tDiff = _this.data.tempTDiff;
          //   _this.data.tempTDiff="";
          // }
          // console.log("threshold = " + JSON.stringify(_this.data.threshold));
          _this.setData({
            threshold: _this.data.old_obj
          });
          
        }else{
   
          if (_this.data.tempTMax != "") {
            // console.log("tempTMax = " + _this.data.tempTMax);
            _this.data.threshold.tMax = _this.data.tempTMax;
            _this.data.tempTMax = "";
          }
          if (_this.data.tempTMin != "") {
            // console.log("tempTMin = " + _this.data.tempTMin);
            _this.data.threshold.tMin = _this.data.tempTMin;
            _this.data.tempTMin = "";
          }
          if (_this.data.tempTDiff != "") {
            // console.log("tempTDiff = " + _this.data.tempTDiff);
            _this.data.threshold.tDiff = _this.data.tempTDiff;
            _this.data.tempTDiff = "";
          }
          // console.log("threshold = " + JSON.stringify(_this.data.threshold));
          _this.setData({
            threshold: _this.data.threshold
          });
        }
       
       
      });
    },
    listenTMaxInput: function (e) {
      this.data.tempTMax = e.detail.value;
      console.log(e)
      // this.data.tempThreshold.tMax = e.detail.value;
      // this.setData({
      //   username: e.detail.value
      // })
    },
    listenTMinInput: function (e) {
      this.data.tempTMin = e.detail.value;
      // this.data.tempThreshold.tMin = e.detail.value;
      // this.setData({
      //   username: e.detail.value
      // })
    },
    listenTDiffInput: function (e) {
      this.data.tempTDiff = e.detail.value;
      // this.data.tempThreshold.tDiff = e.detail.value;
      // this.setData({
      //   username: e.detail.value
      // })
    }
  },
  /**
   * 生命周期
   */
  lifetimes: {
    ready: function () {
    }
  }
})
